import urllib2
import simplejson


target_url = "http://192.168.1.147/three"
album_keys = ('title','artist','releaseDate','duration','artistId','albumId',)

def search():
    response = urllib2.urlopen(target_url).read()

    parse_results(simplejson.loads(response))




def parse_results(data):
    albums_array = []
    # iter over json dict and extract info using tuple album_keys
    for album_raw in (data.get('similarResponse',{}).get('results',{})):
    # container for new album entry
        album_dict = {}
        album_dict['artist'] = return_sub(album_raw.get('album').get('primaryArtists'), 'name')
        album_dict['artistId'] = return_sub(album_raw.get('album').get('primaryArtists'), 'id')
        album_dict['albumId'] = album_raw.get('album').get('ids').get('albumId')
        album_dict['releaseDate'] = album_raw.get('album').get('originalReleaseDate')
        album_dict['duration'] = (album_raw.get('album').get('duration') / 60 )
        album_dict['title'] = album_raw.get('album').get('title')
        album_dict['rating'] = album_raw.get('album').get('rating')
        albums_array.append(album_dict)

    print_results(albums_array)

def return_sub(array, id):
    return array[0].get(id)




def print_results(albums_array):
    for album in albums_array:
        print ''
        for i, key in enumerate(album_keys):
            print '%s: %s' % (key, album.get(album_keys[i]))
    
    
#    print '%s, %s' % (key, element[key])




search()

